/*
	Dracula's Riddle Level 41 Solver
    Copyright (C) 2009  nightmare

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package lvl41;

/**
 * Cog object
 * @author nightmare
 * @version 1.00
 */
public class Cog {
	private int id; 			//Cog id
	private String active;  	//active letter
	private String[] letters;	//Array of letters to read
	private int lastNdx = 0;	//Holds place of last letter read in array letters

	/**
	 * Constructs a Cog
	 * @param cogID an identification number for the created cog
	 * @param activeLetter the letter on the cog that is currently read
	 * @param alpha an array of the letters on each spoke of the cog
	 */
	public Cog(int cogID, String activeLetter, String[] alpha) {
		cogID = id;
		active = activeLetter;
		letters = alpha;
	}

	/**
	 * Spins a cog and assigns the letter of the resulting spoke to active
	 * @param times number of turns a cog should be spun
	 * @param clockwise true if the cog is being spun clockwise, false otherwise
	 * @return the letter on the spoke in the array letters
	 */
	public String spin(int times, boolean clockwise) {
		int ndx = lastNdx;
		if (clockwise) {
			if ((ndx - times) < 0) {
				lastNdx = letters.length + ((ndx - times) % letters.length); //Since modulus does not return a positive value in Java
				active = letters[lastNdx];
			} else {
				lastNdx = ndx - times;
				active = letters[lastNdx];
			}
		} else {
			if ((ndx + times) >= letters.length) {
				lastNdx = (ndx + times) % letters.length;
				active = letters[lastNdx];
			} else {
				lastNdx = ndx + times;
				active = letters[lastNdx];
			}
		}
		String l = read();
		return l;
	}

	/**
	 * Reads the letter on the spoke from the array letters
	 * @return active
	 */
	public String read() {
		return active;
	}
}
